package com.cc.utils.poi;

import com.cc.dao.ColAndRowStyle;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.RichTextString;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * @User c.c
 * @Since 2022/8/12
 * @Time 11:16
 * @Description Excell样式
 */
@Deprecated
public class ExcellPoiStyle {

  public static HashMap<String,ColAndRowStyle> cellStyleMap  = new HashMap<>();

  /**
   * 创建/设置排
   * 目前只能设置行高
   * @author C.c
   * @since 2022-08-12 14:30
   * @param
   * @return
  **/
  public static HSSFRow creatRow(HSSFSheet sheet,int whichRow,int height){
    HSSFRow row = sheet.createRow(whichRow);
    if (height!=0){
      row.setHeight((short)(20*height));
    }
    return row;
  }



  /**
   * 创建/设置列
   *
   * @author C.c
   * @since 2022-08-12 14:30
   * @param row 哪一排
   * @param whichCell  哪一列
   * @param Content 内容
   * @return
   **/
  public static HSSFCell creatCell(HSSFRow row ,int whichCell,Object Content){


    HSSFCell cell = row.createCell(whichCell);


    if (Content instanceof Double){
      Double temp = (double) Content;
      cell.setCellValue(temp);
    }
    if (Content instanceof Date){
      Date temp = (Date) Content;
      cell.setCellValue(temp);
    }
    if (Content instanceof Calendar){
      Calendar temp = (Calendar) Content;
      cell.setCellValue(temp);
    }
    if (Content instanceof String){
      String temp = (String) Content;
      cell.setCellValue(temp);
    }
    if (Content instanceof RichTextString){
      RichTextString temp = (RichTextString) Content;
      cell.setCellValue(temp);
    }
    if (Content instanceof Boolean){
      Boolean temp = (Boolean) Content;
      cell.setCellValue(temp);
    }

    return cell;
  }


  /**
   * 设置字体样式
   * @author C.c
   * @since 2022-08-12 14:18
   * @param fontType 字体样式
   * @param fontSize 字体大小
   * @param horizontalType 水平位置 （HSSFCellStyle里）
   * @param verticalType 垂直位置 (HSSFCellStyle里）
   * @return  HSSFFont
  **/
  public static HSSFCellStyle fontStyle(HSSFWorkbook wb,HSSFCell cell, String fontType, Integer fontSize, Short horizontalType, Short verticalType){

    HSSFCellStyle cellStyle = wb.createCellStyle();

    cellStyle = isExistStyle(cellStyle,cell);
    // 设置字体
    HSSFFont font = wb.createFont();

    if (fontType!=null){
      font.setFontName(fontType);
    }
    if (fontSize!=null){
      font.setFontHeightInPoints((short)fontSize.intValue());
    }
    if (horizontalType!=null){
      cellStyle.setAlignment(horizontalType);
    }
    if (verticalType!=null){
      cellStyle.setVerticalAlignment(verticalType);
    }

    cellStyle.setFont(font);
    putCellAndStyle(cellStyle,cell);
    return cellStyle;
  }
  public static HSSFCellStyle fontStyle(HSSFWorkbook wb,HSSFCell cell){

    return fontStyle(wb,cell,null,null,null,null);
  }


  /**
   * 设置边框样式
   * @author C.c
   * @since 2022-08-12 14:59
   * @param borderStyle 样式类型
   * @return
   **/
  public static void borderStyle(HSSFCell cell,short borderStyle) {

    HSSFCellStyle cellStyle = cell.getCellStyle();
    cellStyle=isExistStyle(cellStyle,cell);
    //设置边框
    cellStyle.setBorderTop(borderStyle);//上边框
    cellStyle.setBorderBottom(borderStyle);//下边框
    cellStyle.setBorderLeft(borderStyle);//左边框
    cellStyle.setBorderRight(borderStyle);//右边框

    putCellAndStyle(cellStyle,cell);
  }



  /**
   * 随内容的多少自动调整表宽
   * @author C.c
   * @since 2022-08-12 15:37
   * @param  title 表头
   * @return
  **/
  public static void AutoExcellWidth(String[] title, HSSFSheet sheet) {
    for (int colNum = 0; colNum < title.length; colNum++) {
      //得到目前宽的长度
      int columnWidth = sheet.getColumnWidth(colNum) / 256;
      for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
        HSSFRow currentRow;
        //当前行未被使用过
        if (sheet.getRow(rowNum) == null) {
          currentRow = sheet.createRow(rowNum);
        } else {
          currentRow = sheet.getRow(rowNum);
        }
        if (currentRow.getCell(colNum) != null) {
          HSSFCell currentCell = currentRow.getCell(colNum);
          if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            int length = currentCell.getStringCellValue().getBytes().length;
            if (columnWidth < length) {
              columnWidth = length;
            }
          }
        }
      }
      //设置宽高
      if(colNum == 0){
        //乘以256，这是怎么回事呢？其实，这个参数的单位是1/256个字符宽度，也就是说，这里是把B列的宽度设置为了(columnWidth-2) 个字符。
        sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
      }else{
        sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
      }
    }
  }



  /**
   * 判断是否存在HSSFCellStyle
   * @author sosmcc
   * @param style 样式
   * @param cell 表格单元
   */
  private static HSSFCellStyle isExistStyle(HSSFCellStyle style,HSSFCell cell){

    String index = cell.getRowIndex()+"_"+ cell.getColumnIndex();

    ColAndRowStyle colAndRowStyle = cellStyleMap.putIfAbsent(index, new ColAndRowStyle(style, cell));

    if (colAndRowStyle!=null){
      return colAndRowStyle.getStyle();
    }
    return style;
  }

  /**
   * 设置单元格样式
   * @author C.c
   * @since 2022-12-09 09:21
   * @param
   * @return
  **/
  public static void setCellStyle(HSSFCellStyle style,HSSFCell cell){
    putCellAndStyle(style,cell);
  }


  /**
   *存放样式
   * @author sosmcc
   * @param
   */
  private static void putCellAndStyle(HSSFCellStyle style,HSSFCell cell){
    String index = cell.getRowIndex()+"_"+ cell.getColumnIndex();
    cellStyleMap.put(index,new ColAndRowStyle(style,cell));
  }

  /**
   * 应用样式
   * @author C.c
   * @since 2022-08-16 17:09
   * @param
   * @return
  **/
  public static void applystyle(){
    cellStyleMap.forEach((k,v)->{
      v.getCell().setCellStyle(v.getStyle());
    });
  }



}
